package sv.gob.mtps.colectivas.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import sv.gob.mtps.colectivas.model.TbEmpleador;

import sv.gob.mtps.colectivas.service.EmpleadorService;
import sv.gob.mtps.colectivas.service.FindService;

@Controller
public class EmpleadorController {
	
	@Autowired
	EmpleadorService empleadorService;
	
	@Autowired
	FindService findService;
	
	@RequestMapping(value = "/admin/showEmpleadores")
	public  String showFondos(@ModelAttribute("empleador") TbEmpleador tbEmpleador,BindingResult result,Map<String, Object> map){
		
		map.put("empleador", new TbEmpleador());		
		map.put("actividadesList", findService.listActividades());
		map.put("empleadoresList", empleadorService.listEmpleador());
		return "admin/empleadores";	
	}
	
	@RequestMapping(value = "/admin/addEmpleador")
	public  String addEmpleador(@ModelAttribute("empleador") TbEmpleador tbEmpleador,BindingResult result,Map<String, Object> map){
		if(!result.hasErrors()){
			empleadorService.addEmpleador(tbEmpleador);
			return "redirect:/admin/showEmpleadores";
		}
		
		return "";
	}
	
	@RequestMapping(value = "/admin/editEmpleador")
	public  String editEmpleador(@ModelAttribute("empleador") TbEmpleador tbEmpleador,BindingResult result,Map<String, Object> map){
		if(!result.hasErrors()){
			empleadorService.editEmpleador(tbEmpleador);
			return "redirect:/admin/showEmpleadores";
		}
		
		return "";
	}

	@RequestMapping(value = "/admin/deleteEmpleador")
	public  String deleteEmpleador(@ModelAttribute("empleador") TbEmpleador tbEmpleador,BindingResult result,Map<String, Object> map){
		if(!result.hasErrors()){
			empleadorService.deleteEmpleador(tbEmpleador.getIdEmpleador());
			return "redirect:/admin/showEmpleadores";
		}
		
		return "";
	}


}
